package cn.edu.bjtu.alex.rewrite.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.edu.bjtu.alex.rewrite.Context;

public class AbstractDocumentAnalyzer {

	private static final Log LOG = LogFactory.getLog(AbstractDocumentAnalyzer.class);
	protected String charSet = "UTF-8";
	private final Set<String> stopwords = new HashSet<String>();
	
	public AbstractDocumentAnalyzer(Context ctx) {
		String  stopWordsDir = ctx.getClass().getResource("/stopwords.txt").getFile();
		if(stopWordsDir != null) {
			load(new File(stopWordsDir));
		}
	}
	
	public boolean isStopword(String word) {
		if(word != null) {
			word = word.trim();
			if(word.isEmpty()) {
				return true;
			} else {
				return stopwords.contains(word);
			}
		}
		return true;
	}

	private void load(File file) {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file.getAbsoluteFile()));
			String word = null;
			while((word = reader.readLine()) != null) {
				word = word.trim();
				if(!word.isEmpty()) {
					if(!stopwords.contains(word)) {
						stopwords.add(word);
					}
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}  catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}		
	}
}
